package cn.fzkj.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/** 通过python脚本获取到一张背景图片. */
public class BackGroundUtil {

    protected static final Logger log = LoggerFactory.getLogger(BackGroundUtil.class);

    /** 通过py脚本返回一张图片的访问地址. */
    public String getImgByPy(String execPath, String path){
        String accessPath = "";
        try{
            // 相当于在cmd中执行了 python xx.py execPath path命令
            String argumens[] = new String[]{"python", execPath, path};
            Process process = Runtime.getRuntime().exec(argumens);
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
            String line = "";
            while ((line = reader.readLine()) != null) {
                accessPath = line;
            }
            reader.close();
            //java代码中的process.waitFor()返回值为0表示我们调用python脚本成功，
            //返回值为1表示调用python脚本失败，这和我们通常意义上见到的0与1定义正好相反
            int re = process.waitFor();
            if (re != 0) log.error("python脚本返回值异常");
        }catch (Exception e){
            e.getLocalizedMessage();
            log.error("python脚本调用错误");
        }
        return "uploaded/" + accessPath;
    }

}
